﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using CoreEx.Common.Ioc;
using LinFu.IoC.Configuration;
using LinFu.IoC.Interfaces;

namespace CoreEx.Wcf.Server.Implementation
{
    /// <summary>
    /// Determines the methods that should be considered an operation contract.
    /// </summary>
    /// <remarks>
    /// The service type is passed into the <see cref="IServiceRequestResult.AdditionalArguments"/> at index 0.
    /// If you need more control over the methods exposed to the service, implement the <see cref="IFactory{T}"/> and 
    /// return an <see cref="IList{T}"/> containing the target <see cref="MethodInfo"/> list.     
    /// By default, every public instance method is returned.
    /// </remarks>
    [PostProcessor]
    public class OperationContractPostProcessor : PostProcessorBase<IList<MethodInfo>>
    {
        public override void Process(IServiceRequestResult result)
        {
            if (result.OriginalResult != null) 
                return;
            Type serviceType = (Type) result.AdditionalArguments[0];
            var methods = serviceType.GetMethods(BindingFlags.Public | BindingFlags.Instance).ToList();
            result.ActualResult = methods;
        }
    }
}